package cn.edu.ujn.c;

import javax.servlet.http.HttpSession;

import org.apache.catalina.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class UserController {
	@GetMapping("/login")
	public String toLogin() {
		return "login";
	}
	@PostMapping("/login")
	public String login(User user, HttpSession session, Model model) {
		String username = user.getUsername();
		String password = user.getPassword();
		if ("huhongli".equals(username)) {
			session.setAttribute("User", user);
			return "main";
		} else {
			model.addAttribute("msg", "用户名密码错误，请重新登录");
			return "login";
		}
	}
	@RequestMapping("/main")
	public String toMain() {
		return "main";
	}
	@RequestMapping("/logout")
	public String logout(HttpSession session) {
		session.invalidate();
		return "login";
	}
}
